package _9991ShuJuFangWenDuiXiang.impl;

import _9991ShuJuFangWenDuiXiang.Student;
import _9991ShuJuFangWenDuiXiang.StudentDao;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @StudentDaoImpl: 数据访问对象接口 实现类
 * @author: ZBoHang
 * @time: 2021/9/18 10:42
 */
public class StudentDaoImpl implements StudentDao {

    List<Student> students;

    public StudentDaoImpl() {
        students = new ArrayList<>();
        Student s1 = new Student("Robert", 0);
        Student s2 = new Student("John", 1);
        students.add(s1);
        students.add(s2);
    }

    @Override
    public List<Student> getAllStudent() {
        return students;
    }

    @Override
    public Student getStudent(int rollNo) {
        return students.get(rollNo);
    }

    @Override
    public void updateStudent(Student student) {
        for (Student s : students) {
            if (s.getRollNo() == student.getRollNo()) {
                s.setRollNo(student.getRollNo());
                s.setName(student.getName());
            }
        }
        System.out.println("修改为 --> " + student);
    }

    @Override
    public void deleteStudent(Student student) {
        students.removeIf(new Predicate<Student>() {
            @Override
            public boolean test(Student s) {
                return s.getRollNo() == student.getRollNo();
            }
        });
        System.out.println("删除 --> " + student);
    }
}
